-
Notifications
You must be signed in to change notification settings - Fork 5.7k
[SOT][FasterGuard] ShapeMatchGuard
support dynamic shape
#72564
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SOT][FasterGuard] ShapeMatchGuard
support dynamic shape
#72564
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR updates the ShapeMatchGuard to support dynamic shapes for performance optimizations.
- Refactors the constructor to accept a vector of py::object and converts it to int64_t values.
- Modifies the shape-checking logic in the guard implementation.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
paddle/fluid/pybind/sot/guards.h | Adjusted constructor and changed internal type usage. |
paddle/fluid/pybind/sot/guards.cc | Updated shape checking logic to validate non-positive dimensions. |
paddle/fluid/pybind/sot/guards.cc
Outdated
@@ -141,7 +141,7 @@ bool ShapeMatchGuard::check(PyObject* value) { | |||
return false; | |||
} | |||
for (size_t i = 0; i < shape.size(); ++i) { | |||
if (expected_[i] && shape[i] != *expected_[i]) { | |||
if (shape[i] <= 0 || shape[i] != expected_[i]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The updated condition checks for non-positive values and fails dynamic shapes that could be represented by a negative or zero value. To support dynamic shapes, consider revising the condition to allow for dynamic dimensions (e.g., checking if expected_[i] is set to a dynamic indicator such as -1 before comparing equality).
if (shape[i] <= 0 || shape[i] != expected_[i]) { | |
if (shape[i] <= 0 || (expected_[i] != -1 && shape[i] != expected_[i])) { |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds dynamic shape support to the ShapeMatchGuard, improving performance by allowing flexible shape validation.
- Change the ShapeMatchGuard constructor to accept a vector of py::object and represent dynamic dimensions with a sentinel value.
- Replace the std::optional<int64_t> storage with std::vector<int64_t>, using -1 as the indicator for dynamic dimensions.
- Update the shape-check logic in guards.cc accordingly.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
paddle/fluid/pybind/sot/guards.h | Updated constructor and internal representation to support dynamic shape |
paddle/fluid/pybind/sot/guards.cc | Modified shape checking logic to accommodate dynamic shape representation |
paddle/fluid/pybind/sot/guards.h
Outdated
if (py::isinstance<py::int_>(s)) { | ||
expected_.push_back(s.cast<int64_t>()); | ||
} else { | ||
expected_.push_back(-1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Consider replacing the magic number -1 with a named constant (e.g., kDynamicDimension) to improve code readability and maintainability.
expected_.push_back(-1); | |
expected_.push_back(kDynamicDimension); |
Copilot uses AI. Check for mistakes.
paddle/fluid/pybind/sot/guards.cc
Outdated
@@ -141,7 +141,8 @@ bool ShapeMatchGuard::check(PyObject* value) { | |||
return false; | |||
} | |||
for (size_t i = 0; i < shape.size(); ++i) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Adding a brief comment clarifying that -1 is used as a sentinel for dynamic dimensions would help maintainers understand the control flow.
for (size_t i = 0; i < shape.size(); ++i) { | |
for (size_t i = 0; i < shape.size(); ++i) { | |
// -1 is used as a sentinel value for dynamic dimensions, allowing any positive size to match. |
Copilot uses AI. Check for mistakes.
paddle/fluid/pybind/sot/guards.h
Outdated
if (py::isinstance<py::int_>(s)) { | ||
expected_.push_back(s.cast<int64_t>()); | ||
} else { | ||
expected_.push_back(-1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么把原来能够用类型保护的 optional<int64_t>
改成了没有类型保护的 int64_t
?
如果觉得 shape
有传入 -1
的风险,就直接规避掉,构造函数里直接 PADDLE_ENFORCE_GE(shape[i], 0, ...)
去掉类型安全绝对是很蠢的操作,没错说的就是整个框架用 -1 表示 dynamic dim 这种操作
另外 |
Sorry to inform you that 308bdc8's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces support for dynamic shape matching in ShapeMatchGuard and NumPyArrayShapeGuard as part of performance optimizations.
- Updated constructors in both ShapeMatchGuard and NumPyArrayShapeGuard to directly assign values from py::object without using std::make_optional.
- Introduced a CHECK_SHAPE macro in guards.cc to centralize shape comparisons, replacing duplicate loop logic.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
paddle/fluid/pybind/sot/guards.h | Simplified constructors in ShapeMatchGuard and NumPyArrayShapeMatchGuard by using direct assignment. |
paddle/fluid/pybind/sot/guards.cc | Added CHECK_SHAPE macro to reduce duplicate shape-checking logic. |
Comments suppressed due to low confidence (1)
paddle/fluid/pybind/sot/guards.cc:63
- [nitpick] Using a macro for shape checking can obscure control flow due to embedded return statements. Consider using an inline function to enhance readability and debugging clarity.
#define CHECK_SHAPE(expected, ndim, actual_shape) { ... }
paddle/fluid/pybind/sot/guards.h
Outdated
explicit ShapeMatchGuard(const std::vector<py::object>& shape) { | ||
expected_.resize(shape.size()); | ||
for (size_t i = 0; i < shape.size(); ++i) { | ||
if (py::isinstance<py::int_>(shape[i]) && shape[i].cast<int64_t>() > 0) { | ||
expected_[i] = std::make_optional(shape[i].cast<int64_t>()); | ||
expected_[i] = shape[i].cast<int64_t>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Direct assignment to a std::optional relies on its implicit construction from int64_t. Consider explicitly using std::make_optional for clarity and consistency.
expected_[i] = shape[i].cast<int64_t>(); | |
expected_[i] = std::make_optional(shape[i].cast<int64_t>()); |
Copilot uses AI. Check for mistakes.
paddle/fluid/pybind/sot/guards.h
Outdated
explicit NumPyArrayShapeMatchGuard(const std::vector<py::object>& shape) { | ||
expected_.resize(shape.size()); | ||
for (size_t i = 0; i < shape.size(); ++i) { | ||
if (py::isinstance<py::int_>(shape[i]) && shape[i].cast<int64_t>() > 0) { | ||
expected_[i] = std::make_optional(shape[i].cast<int64_t>()); | ||
expected_[i] = shape[i].cast<int64_t>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Direct assignment to a std::optional here relies on implicit conversion. Consider explicit wrapping with std::make_optional for clarity.
expected_[i] = shape[i].cast<int64_t>(); | |
expected_[i] = std::make_optional(shape[i].cast<int64_t>()); |
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里 copilot 的建议有问题么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
两者在性能上区别不大,就没改了,等 review 呢 qwq~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
本来就不是性能差异,这里只是代码写法差异吧?显式优于隐式
paddle/fluid/pybind/sot/guards.h
Outdated
explicit NumPyArrayShapeMatchGuard(const std::vector<py::object>& shape) { | ||
expected_.resize(shape.size()); | ||
for (size_t i = 0; i < shape.size(); ++i) { | ||
if (py::isinstance<py::int_>(shape[i]) && shape[i].cast<int64_t>() > 0) { | ||
expected_[i] = std::make_optional(shape[i].cast<int64_t>()); | ||
expected_[i] = shape[i].cast<int64_t>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里 copilot 的建议有问题么?
paddle/fluid/pybind/sot/guards.cc
Outdated
@@ -60,6 +60,20 @@ static inline PyObject* PyObject_CallOneArg(PyObject* func, PyObject* arg) { | |||
} \ | |||
} | |||
|
|||
#define CHECK_SHAPE(expected, ndim, actual_shape) \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里用函数不可以么?一定要用宏么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
两个类型不太一样
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用模板呢?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Category
Performance Optimization
PR Types
Performance
Description
ShapeMatchGuard
支持动态 shape